import { instance } from "@/plugins/request";

/**
 * @description 封装 get 请求
 * @param {String} url 请求地址
 * @param {Object} params 请求参数
 * @param {Object} headers 请求头
 * @return {Promise}
 **/
export const httpGet = (url, params = undefined, headers = undefined) => {
	const config = {
		method: "get",
		params,
		url
	};
	if (!params) delete config.params;
	if (headers) config.headers = headers;
	return instance(config);
};

/**
 * @description 封装 post 请求
 * @param {String} url 请求地址
 * @param {Object} data 请求参数
 * @param {Object} headers 请求头
 * @return {Promise}
 */
export const httpPost = (url, data, headers = undefined) => {
	const config = {
		data,
		method: "post",
		url
	};
	if (!data) delete config.data;
	if (headers) config.headers = headers;
	return instance(config);
};

/**
 * @description 封装 put 请求
 * @param {String} url 请求地址
 * @param {Object} data 请求参数
 * @param {Object} headers 请求头
 * @return {Promise}
 */
export const httpPut = (url, data, headers = undefined) => {
	const config = {
		data,
		method: "put",
		url
	};
	if (!data) delete config.data;
	if (headers) config.headers = headers;
	return instance(config);
};

/**
 * @description 封装 delete 请求
 * @param {String} url 请求地址
 * @param {Object} params 路径参数
 * @param {Object} headers 请求头
 * @return {Promise}
 */
export const httpDelete = (url, params = undefined, headers = undefined) => {
	const config = {
		method: "delete",
		params,
		url
	};
	if (!params) delete config.params;
	if (headers) config.headers = headers;
	return instance(config);
};
